package com.ruoyi.project.cms.type.controller;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.cms.type.domain.CMSType;
import com.ruoyi.project.cms.type.service.ICMSTypeService;
import com.ruoyi.project.system.post.domain.Post;
import com.sun.org.apache.xerces.internal.impl.dtd.models.CMStateSet;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *
 * 公告类别管理器
 * 主要完成公告类别的功能
 *
 *
 */
@Controller
@RequestMapping("/cms/type")
public class CMSTypeController extends BaseController {

    @Autowired
    private ICMSTypeService icmsTypeService;


    private String prefix = "cms/type";
    @GetMapping
    @RequiresPermissions("cms:type:view")
    public String type(){
        return prefix+"/type";
    }


    @RequiresPermissions("cms:type:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CMSType cmsType)
    {
        startPage();
        List<CMSType> list = icmsTypeService.selectCMSTypeList(cmsType);
        return getDataTable(list);
    }


    /**
     * 新增公告类别
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }



    /**
     * 新增公告类别管理
     *
     * @Validated SpringMVC的表单注解
     *
     */
    @RequiresPermissions("cms:type:add")
    @Log(title = "公告类别管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated CMSType cmsType)
    {
        /*后台校验，防止前端绕过表单js校验，直接通过工具提交数据*/
        if (UserConstants.POST_NAME_NOT_UNIQUE.equals(icmsTypeService.checkTypeNameUnique(cmsType)))
        {
            return error("新增公告类别'" + cmsType.getTypeName() + "'失败，公告类别名称已存在");
        }

        return toAjax(icmsTypeService.insertCMSType(cmsType));
    }


    /**
     * 修改公告类别
     */
    @GetMapping("/edit/{typeId}")
    public String edit(@PathVariable("typeId") Long typeId, ModelMap mmap)
    {
        //根据修改id查询出对应的公告类别对象，回显
        CMSType cmsType =  icmsTypeService.sleectByCMSTypeId(typeId);
        System.out.println("cmsType :"+cmsType);
        mmap.put("cmsType",cmsType);
        return prefix + "/edit";
    }

    /**
     * 修改保存公告分类
     */
    @RequiresPermissions("cms:type:edit")
    @Log(title = "公告类别管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated CMSType cmsType)
    {
        if (UserConstants.POST_NAME_NOT_UNIQUE.equals(icmsTypeService.checkTypeNameUnique(cmsType)))
        {
            return error("修改公告类别'" + cmsType.getTypeName() + "'失败，公告类别名称已存在");
        }

        int row = icmsTypeService.updateCMSType(cmsType);

        return toAjax(row);
    }


    @RequiresPermissions("cms:type:remove")
    @Log(title = "公告类别管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(Long[] ids)
    {
        try
        {

            int rows = icmsTypeService.deleteByIds(ids);

            return toAjax(rows);
        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }
    }




    /**
     * 校验公告类别名称
     */
    @PostMapping("/checkTypeNameUnique")
    @ResponseBody
    public String checkTypeNameUnique(CMSType cmsType)
    {
        return icmsTypeService.checkTypeNameUnique(cmsType);
    }

}


